package cn.itsource.hrm.userdetails;

import cn.itsource.hrm.clients.SysmanageServiceClient;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.domain.Permission;
import cn.itsource.hrm.exception.ValidUtils;
import cn.itsource.hrm.mapper.LoginUserMapper;
import cn.itsource.hrm.mapper.PermissionMapper;
import cn.itsource.hrm.vo.UserContextInfo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private LoginUserMapper loginUserMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private SysmanageServiceClient sysmanageServiceClient;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //加载认证信息
        List<LoginUser> loginUsers = loginUserMapper
                .selectList(new EntityWrapper<LoginUser>().eq("username", username));
        ValidUtils.assertListNotNull(loginUsers, "请输入用户名或密码！");
        //加载权限信息
        LoginUser loginUser = loginUsers.get(0);
        //yaosang110   new Xxx();
        UserContextInfo userContextInfo = new UserContextInfo();
        userContextInfo.setLoginUserId(loginUser.getId());
        userContextInfo.setLoginUsername(loginUser.getUsername());
        if (loginUser.getType()==0){ //管理员

            //只有管理员才有权限控制，用户登录就能他能操作的
            List<Permission> permissions = permissionMapper.loadPermissionsByUserId(loginUser.getId());
            List<String> permissionSns = new ArrayList<>();
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            permissions.forEach(permission -> {
                String sn = permission.getSn();
                authorities.add(new SimpleGrantedAuthority(sn));
                permissionSns.add(sn);
            });
            // 远程调用Sysmanage的接口 loginId--employee-tenantId-TenantName
            Map<String, Object> result = sysmanageServiceClient
                    .getEmpAndTenant(loginUser.getId());
            Long tenantId = Long.valueOf(result.get("tenantId").toString());
            String tenantName = result.get("tenantName").toString();
            Long employeeId = Long.valueOf(result.get("employeeId").toString());
            String empUsername = result.get("empUsername").toString();
            userContextInfo.setTenantId(tenantId);
            userContextInfo.setTenantName(tenantName);
            userContextInfo.setEmployeeId(employeeId);
            userContextInfo.setEmpUsername(empUsername);
            userContextInfo.setPermissions(permissionSns);
            return new User(JSON.toJSONString(userContextInfo),loginUser.getPassword(),authorities);
        }else{
            //@TODO 自己做
            userContextInfo.setUserId(null);
            userContextInfo.setUsername(null);
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            return new User(JSON.toJSONString(userContextInfo),loginUser.getPassword(),authorities);
        }

    }
}
